package com.example.demo.service;

//import com.shuyun.channel.shard.ShardConfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.saas.dao.MybatisDao;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class ChannelService {
    public Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private MybatisDao myBatisDao;

   /* public List<ShardConfig> shardConfig(String topic) {
        return myBatisDao.getList("shardConfig", topic);
    }*/

    public void insertCompleteTransitAndDeleteCurrent(List<Map<String, Object>> loadData, List<Integer> pkidList) throws Exception {
        //插入结果到transitResult
        Map<String, Object> parameterMapT = new HashMap<>();
        parameterMapT.put("list", loadData);
        myBatisDao.save("ChannelMapper.loadTransitResult", parameterMapT);
        //删除transitCurrent
        Map<String, Object> parameterMap = new HashMap<>();
        parameterMap.put("pkidList", pkidList);
        myBatisDao.save("ChannelMapper.deleteCurrent", parameterMap);
    }

    public void insertMessage(String loadSqlKey, List<Map<String, Object>> loadData) throws Exception {
        Map<String, Object> parameterMapT = new HashMap<>();
        parameterMapT.put("list", loadData);
        myBatisDao.save(loadSqlKey, parameterMapT);
    }

    public void insertBackAccount(List<Map<String, Object>> loadData) throws Exception {
        Map<String, Object> parameterMapT = new HashMap<>();
        parameterMapT.put("list", loadData);
        myBatisDao.save("ChannelMapper.insertBackAccount", parameterMapT);
    }

    public void updateTransitCurrentThreadid(String threadId, Integer gatewayId, Integer limitNum) {
        Map<String, Object> parameterMap1 = new HashMap<>();
        parameterMap1.put("gatewayId", gatewayId);
        parameterMap1.put("limitNum", limitNum);
        List<Long> pkidList;
        if (gatewayId.equals(4)) {//优惠券特殊处理，满足量少先发
            pkidList = myBatisDao.getList("ChannelMapper.getCouponTransitCurrentPkidLevel9", parameterMap1);
            if (pkidList.size() == 0) {
                pkidList = myBatisDao.getList("ChannelMapper.getCouponTransitCurrentPkidSmallLevel0", parameterMap1);
                if (pkidList.size() == 0) {
                    pkidList = myBatisDao.getList("ChannelMapper.getCouponTransitCurrentPkidLevel5", parameterMap1);
                    if (pkidList.size() == 0) {
                        pkidList = myBatisDao.getList("ChannelMapper.getCouponTransitCurrentPkidLevel0", parameterMap1);
                    }
                }
            }
        } else if (gatewayId.equals(904)) {//定向优惠特殊处理，满足量少先发
            pkidList = myBatisDao.getList("ChannelMapper.getUmpTransitCurrentPkidLevel9", parameterMap1);
            if (pkidList.size() == 0) {
                pkidList = myBatisDao.getList("ChannelMapper.getUmpTransitCurrentPkidLevel5", parameterMap1);
                if (pkidList.size() == 0) {
                    pkidList = myBatisDao.getList("ChannelMapper.getUmpTransitCurrentPkidLevel0", parameterMap1);
                }
            }
        } else {
            pkidList = myBatisDao.getList("ChannelMapper.getTransitCurrentPkid", parameterMap1);
        }
        if (pkidList.size() > 0) {
            Map<String, Object> parameterMap2 = new HashMap<>();
            parameterMap2.put("threadId", threadId);
            parameterMap2.put("pkidList", pkidList);
            myBatisDao.save("ChannelMapper.updateTransitCurrentThreadid", parameterMap2);
        }
    }

    /**
     * 获取竞争到的发送任务
     *
     * @param sqlKey    哪种类型
     * @param gatewayId 哪个通道
     * @param competeId 用的什么标识符
     */
    public List<Map<String, Object>> getTransitCurrent(String sqlKey, Integer gatewayId, String competeId) {
        Map<String, Object> parameterMap = new HashMap<>();
        parameterMap.put("threadId", competeId);
        parameterMap.put("gatewayId", gatewayId);
        return myBatisDao.getList(sqlKey, parameterMap);
    }

    public Map<String, Object> getSendConfigIfGatewayValid(int gatewayId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("pkid", gatewayId);
        return myBatisDao.getSingleRow("ChannelMapper.getSendConfigIfValid", paramMap);
    }

    public List<Integer> getWaitSendGatewayId() {
        return myBatisDao.getList("ChannelMapper.getWaitSendGatewayId", null);
    }

    public void competeSendConfigRight(String gatewayIdStr, String serid, String seridCondition) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("pkid", gatewayIdStr);
        paramMap.put("serid", serid);
        paramMap.put("seridCondition", seridCondition);
        myBatisDao.save("ChannelMapper.competeSendConfigRight", paramMap);
    }

    public String getSendConfigRight(String gatewayIdStr, String serid) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("pkid", gatewayIdStr);
        paramMap.put("serid", serid);
        return myBatisDao.getSingleRow("ChannelMapper.getSendConfigRight", paramMap);
    }

    public void updateOverTimeCheckLevelTask() {
        myBatisDao.save("ChannelMapper.updateOverTimeCheckLevelTask", null);
    }

    public void updateAdminGatewayTestDetail(Map<String, Object> testDetailList) {
        myBatisDao.save("ChannelMapper.updateAdminGatewayTestDetail", testDetailList);
    }

    public void updateGatewayTestOwner(Map<String, Object> next) {
        myBatisDao.save("ChannelMapper.updateGatewayTestOwner", next);
    }


    public boolean getIfOwnTest(Map<String, Object> detail) {
        return myBatisDao.getSingleRow("ChannelMapper.getIfOwnTestDetail", detail) != null;
    }

    public void updateGatewayHolding(Map<String, Object> gidAndSerIdAndTime) {
        myBatisDao.save("ChannelMapper.refreshGatewayLifeTime", gidAndSerIdAndTime.get("gatewayId"));
    }

    public void releaseGatewayHolding(Map<String, Object> gidAndSerId) {
        myBatisDao.save("ChannelMapper.releaseSendConfigRight", gidAndSerId.get("gatewayId"));

    }
}
